package com.jimmy.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * @version 1.0
 * @ ClassName MyAdvice2
 * <p> 测试获取参数与返回值
 * @ author JIMMY
 * @ Date 2020/10/31 0031 上午 6:56
 */
public class MyAdvice2 {
    public void before(JoinPoint joinPoint){
        //可以获取参数
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println("获取参数"+ args[i]);
        }
        System.out.println("before");
    }
    public void after(JoinPoint joinPoint){
        //可以获取参数
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println("获取参数"+ args[i]);
        }
        System.out.println("after");
    }
    public void afterReturning(JoinPoint joinPoint,Object abc){
        //可以获取参数
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println("获取参数"+ args[i]);
        }
        System.out.println("返回值为"+abc);
        System.out.println("afterReturning");
    }
    public void around(ProceedingJoinPoint proceedingJoinPoint){
        try {
            Object[] args = proceedingJoinPoint.getArgs();
            for (int i = 0; i < args.length; i++) {
                System.out.println("获取参数"+ args[i]);
            }
//            proceedingJoinPoint.proceed();
            //动动手脚改变参数或者可以做校验
            Object[] objects = new Object[2];
            objects[0] = 2;
            objects[1] = "test";
            System.out.println("改变参数后");
           proceedingJoinPoint.proceed(objects);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("around");
    }
    public void around2(ProceedingJoinPoint proceedingJoinPoint){
        try {
            Object[] args = proceedingJoinPoint.getArgs();
            for (int i = 0; i < args.length; i++) {
                System.out.println("获取参数"+ args[i]);
            }
            System.out.println("before");
            Object proceed = proceedingJoinPoint.proceed();
            System.out.println("after");
            System.out.println("拦截到返回值为"+proceed);

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

    }
    public void afterThrowing(JoinPoint joinPoint){
        //可以获取参数
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println("获取参数"+ args[i]);
        }
        System.out.println("afterThrowing");
    }
}
